Coding for Kids in Python: Create Your First Game with Python by Ellen Tale

Coding for Kids in Python: Create Your First Game with Python by Ellen Tale

Author:Ellen Tale [Tale, Ellen]
Language: eng
Format: azw3, epub
Published: 2020-06-20T16:00:00+00:00


The ‘for’ loop

The ‘for’ loop is a python iterator, just like the while loop. It works on strings, lists, tuples, and dictionaries as well as other built-in objects that can be iterated through

In this lesson, we’ll be learning how to use the ‘for’ loop to solve fundamental mathematics problems and doing other exciting stuff.

Don’t be scared because I mentioned mathematics; you do not need to know a lot of mathematics before you can use the ‘for’ loop.

The generic format of a ‘for’ loop can be seen below.

for target in objects:

statements

Before we proceed to learn more about ‘for’ loops, there is an actual need to know about the ‘break’, ‘continue’ and pass statements

Anywhere you see the ‘break’ statement in a loop or a conditional statement, it is used to terminate or exit that loop or conditional statement

Let’s take an example

for x in range(1,8):

if x == 5:

break

else:

print(x)

The result can be seen below that when x actually became 5, the ‘for’ loop was terminated.

Python 3.8.1 (tags/v3.8.1:1b293b6, Dec 18 2019, 22:39:24) [MSC v.1916 32 bit (Intel)] on win32

Type "help", "copyright", "license()" for more information.

>>>

==================== RESTART: C:\Users\uf\Desktop\new.py =====================

1

2

3

4

>>>

Also, wherever you see the ‘continue’ statement in a loop or conditional statement, it is used to jump to the top or beginning of that loop or conditional statement.

Let’s also take an example here

for x in range(1,8):

if x == 5:

continue

else:

print(x)

It can be seen from the result below that the code jumps the number 5 and proceed to print other numbers.

Note that range(1, 8) is a built-in function which contains number 1 to 7 which could be iterated though using a loop.

Python 3.8.1 (tags/v3.8.1:1b293b6, Dec 18 2019, 22:39:24) [MSC v.1916 32 bit (Intel)] on win32

Type "help", "copyright", "license()" for more information.

>>>

==================== RESTART: C:\Users\uf\Desktop\new.py =====================

1

2

3

4

6

7

>>>

The ‘pass’ statement is often used when there is nothing useful to say or do in a conditional or loop statement. It is commonly used to code an empty body for a compound statement. For example

number = 3

if number == 3:

pass

else:

print ('The number is not 3')

It can be seen from the result below that the code did precisely nothing.

Python 3.8.1 (tags/v3.8.1:1b293b6, Dec 18 2019, 22:39:24) [MSC v.1916 32 bit (Intel)] on win32

Type "help", "copyright", "license()" for more information.

>>>

==================== RESTART: C:\Users\uf\Desktop\new.py =====================

>>>

Let’s take a few practical examples

Let’s try to calculate the mean of different numbers in a list using the ‘for’ loop. I am sure you all remember what a list is and how to use a list and perform different operations using a list from previous lessons.

From our mathematics class in school, do we still remember how to calculate mean? If the answer is No, there is no need to worry, I explain it to you.

In order to calculate the mean of some scores, you have to find out how many scores there are to find the way. Say you have to find the way of these scores, “2, 6, 8, 12, 16, 20, 10, 5, 8, 7”

You count the number of ratings given, and right here, we have 10 scores.

The next step is to add



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.
Popular ebooks
Adventures In Raspberry Pi by Philbin Carrie Anne(600)
Python for Kids by Jason R. Briggs(571)
Scratch For Kids For Dummies by Derek Breen(531)
Coding for Kids in Python: Create Your First Game with Python by Ellen Tale(458)
Coding for Kids: Beginners Guide with Fun and Easy Activities to Learn Coding Step by Step by Weber Simon(412)
Learn Coding: Python, Java, PHP Complete Guide, and Other of the Most Recommended Programming Languages for Beginners in use Today by Wired Tim(409)
Star Wars Coding Projects by Jon Woodcock(404)
Writing Computer Code by Chris Minnick & Eva Holland(368)
Coding for Kids: Scratch: Fun & Easy Step-by-Step Visual Guide to Building Your First 10 Projects (Great for 7+ year olds!) by Nathan Jobs(331)
Coding Animation and Games with Scratch: A beginner’s guide for kids to creating animations, games and coding, using the Scratch computer language by Zako & Mukherjee Sumita(292)
Kids Learning Javascript: Kids learn coding like playing games by Yang Hu(292)
C++ and Algorithmic Thinking for the Complete Beginner: Learn to Think Like a Programmer (Part 1 of 5) by Aristides Bouras & Loukia Ainarozidou(284)
Beginning Microsoft Small Basic - A Computer Programming Tutorial by Conrod Philip & Tylee Lou(239)
Coding for Kids in Scratch Python - 2 Books in 1 -: Create Your First Games by Tale Ellen(234)
Coding for Kids: Scratch 3: A Step by Step Visual Guide for Beginners to Learn How to Code with Guided Activities and Build Your Own Computer Games(Includes 25 Coding Challenges With Keys) by Raymond Deep(212)
Python for Kids: The New Step-by-Step Parent-Friendly Programming Guide With Detailed Installation Instructions. To Stimulate Your Kid With Awesome Games, Activities And Coding Projects by Deep Raymond(211)
Can You Crack the Code? by Ella Schwartz(174)